<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
	xmlns="http://www.w3.org/1999/xhtml"
	xmlns:c="livcos.org/ns/cosmos"
	xmlns:f="livcos.org/ns/xslt/function"
	xmlns:da="livcos.org/ns/cosmos/data-access"
	exclude-result-prefixes="c f da">
	
	<xsl:import href="../../common/base.xsl"/>

  <xsl:param name="ref" select="'/hsr.ch/DbDoc/data/sas'"/>

	<xsl:template match="/">
		<html>
			<head>
				<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
				<title>Non-unique IDs</title>
				<link href="../css/global.css" rel="stylesheet" type="text/css" />
			</head>
			<body>
				<h3>Objects in <xsl:value-of select="$ref"/> holding multiple entities with the same ID</h3>
				<table border="1" cellspacing="0" cellpadding="2" style="font-size: 80%">
					<tbody>
						<xsl:apply-templates select="doc(concat($ref, '.data'))/c:*//c:*"/>
					</tbody>
				</table>
			</body>
		</html>
	</xsl:template>
	
	<xsl:template match="c:*">
		<xsl:variable name="c" select="* except c:* except da:*"/>
		<xsl:if test="$c">
			<xsl:variable name="r">
				<xsl:variable name="ids" select="$c//@id"/>
				<xsl:for-each select="distinct-values($ids)">
					<id val="{.}" count="{count($ids[. = current()])}"/>
				</xsl:for-each>
			</xsl:variable>
			<xsl:variable name="doubleIds" select="$r/*[@count > 1]"/>
			<xsl:if test="$doubleIds">
				<tr>
					<td>
						<xsl:value-of select="f:object-id(.)"/>
					</td>
					<td>
						<xsl:for-each select="$doubleIds">
							<xsl:if test="position() > 1">, </xsl:if>
							<xsl:value-of select="@val"/> (<xsl:value-of select="@count"/>
							<xsl:text>)</xsl:text>
						</xsl:for-each>
					</td>
				</tr>
			</xsl:if>
		</xsl:if>
	</xsl:template>
	
	<xsl:template match="id">
	</xsl:template>
	
</xsl:stylesheet>
